package com.example.springboot.system.controller;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.resp.Result;
import com.example.springboot.common.utils.JwtToken;
import com.example.springboot.system.entity.ThumbsEntity;
import com.example.springboot.system.mapper.ThumbsMapper;
import com.example.springboot.system.service.ThumbsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 点赞表 前端控制器
 * </p>
 *
 * @author jiang
 * @since 2025-04-17
 */
@RestController
@RequestMapping("/thumbsEntity")
public class ThumbsController {

    private final ThumbsService service;
    private final ThumbsMapper mapper;

    public ThumbsController(ThumbsService service, ThumbsMapper mapper) {
        this.service = service;
        this.mapper = mapper;
    }

    @JwtToken
    @Operation(summary = "列表")
    @GetMapping("/thumbsList")
    public Result<?> thumbsList() {
        return Result.success(service.list());
    }

    @JwtToken
    @Operation(summary = "获取")
    @PostMapping("/thumbsId")
    public Result<?> thumbsId(@RequestParam Integer id) {
        return Result.success(mapper.selectById(id));
    }

    @JwtToken
    @Operation(summary = "列表分页")
    @PostMapping("/thumbsListPage")
    public Result<?> thumbsListPage(@RequestParam Integer page, @RequestParam Integer pageSize) {
        Page<ThumbsEntity> rowPage = new Page<>(page, pageSize);
        QueryWrapper<ThumbsEntity> wrapper = new QueryWrapper<>();
        rowPage = mapper.selectPage(rowPage, wrapper);
        return Result.success(rowPage);
    }

    @Data
    static class Thumbs {
        private Integer newsId;
        private Integer userId;
        private Date current;
    }

    @JwtToken
    @Operation(summary = "新增")
    @PostMapping("/thumbsAppend")
    public Result<?> thumbsAppend(@RequestBody ThumbsController.Thumbs thumbs) {
        ThumbsEntity entity = new ThumbsEntity();
        BeanUtils.copyProperties(thumbs, entity);
        return Result.success(service.save(entity));
    }

    @JwtToken
    @Operation(summary = "删除")
    @PostMapping("/thumbsDelete")
    public Result<?> thumbsDelete(@RequestParam Integer id) {
        return Result.success(service.removeById(id));
    }

    @JwtToken
    @Operation(summary = "编辑")
    @PostMapping("/thumbsEditor")
    public Result<?> thumbsEditor(@RequestBody ThumbsEntity thumbsEntity) {
        QueryWrapper<ThumbsEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("id", thumbsEntity.getId());
        return Result.success(service.updateById(thumbsEntity));
    }

    @JwtToken
    @Operation(summary = "搜索")
    @PostMapping("/thumbsSearch")
    public Result<?> thumbsSearch(@RequestParam String name) {
        QueryWrapper<ThumbsEntity> wrapper = new QueryWrapper<>();
        wrapper.like("name", name);
        return Result.success(mapper.selectList(wrapper));
    }


}
